package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.dto.SpuAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Spu;
import cn.tedu.csmall.product.pojo.entity.SpuDetail;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ISpuService;
import cn.tedu.csmall.product.util.IdUtils;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SpuServiceImpl implements ISpuService {

    @Autowired
    private IdUtils idUtils;
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private SpuDetailMapper spuDetailMapper;

    @Override
    public void addNew(SpuAddNewDTO spuAddNewDTO) {
        log.debug("开始处理【添加SPU】的业务,参数: {}", spuAddNewDTO);
        // 检查品牌：是否存在，是否启用
        long brandId = spuAddNewDTO.getBrandId();
        BrandStandardVO brand = brandMapper.getStandardById(brandId);
        if (brand == null){
            String message = "添加失败!品牌数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if (brand.getEnable() == 0){
            String message = "添加失败!品牌未启用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        // 检查类别：是否存在，是否启用，是否不包含子级
        long categoryId = spuAddNewDTO.getCategoryId();
        CategoryStandardVO category = categoryMapper.getStandardById(categoryId);
        if (category == null){
            String message = "添加失败!类别数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        if (category.getEnable() == 0){
            String message = "添加失败!类别已被禁用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        if (category.getIsParent() == 1){
            String message = "添加失败!类别包含子级类别";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,message);
        }
        // 检查相册：是否存在
        long albumId = spuAddNewDTO.getAlbumId();
        AlbumStandardVO album = albumMapper.getStandardById(albumId);
        if (album == null){
            String message = "添加失败!相册数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        long spuId = idUtils.generateSpuId();
        // 创建Spu对象
        Spu spu = new Spu();
        // 复制属性值
        BeanUtils.copyProperties(spuAddNewDTO,spu);
        // 补全属性值：id >>> 暂时随便写
        spu.setId(spuId);
        // 补全属性值：brand_name, category_name >>> 此前检查时的查询结果
        spu.setBrandName(brand.getName());
        spu.setCategoryName(category.getName());
        // 补全属性值：sales, comment_count, positive_comment_count >>> 0
        spu.setSales(0);
        spu.setCommentCount(0);
        spu.setPositiveCommentCount(0);
        // 补全属性值：is_deleted, is_published, is_new_arrival, is_recommend >>> 0
        spu.setIsDeleted(0);
        spu.setIsPublished(0);
        spu.setIsNewArrival(0);
        spu.setIsRecommend(0);
        // 补全属性值：is_checked >>> 0, check_user >>> null, gmt_check >>> null
        spu.setIsChecked(0);
        // 插入Spu数据
        int rows = spuMapper.insert(spu);
        if (rows != 1){
            String message = "添加SPU失败,服务器忙,请稍后再尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
        // 创建SpuDetail对象
        SpuDetail spuDetail = new SpuDetail();
        // 补全属性值：spu_id >>> 与以上Spu的ID相同
        spuDetail.setSpuId(spu.getId());
        // 补全属性值：detail >>> 来自参数
        spuDetail.setDetail(spuAddNewDTO.getDetail());
        // 插入SpuDetail数据
        rows = spuDetailMapper.insert(spuDetail);
        if (rows != 1){
            String message = "添加SPU失败,服务器忙,请稍后再尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT,message);
        }
    }
}
